package org.iipg.data.sync.crawler;

import java.io.IOException;

import org.iipg.data.sync.common.SQLGetter;
import org.iipg.data.sync.common.WorkerProp;
import org.iipg.data.sync.common.util.StoreUtil;
import org.json.JSONArray;
import org.json.JSONObject;

public class SQLWorker extends Worker {

	public SQLWorker(WorkerProp props) {
		super(props);
	}

	@Override
	public void run() {
		try {
			// getRecords
			SQLGetter getter = new SQLGetter(getProps().getSourceDS());
			JSONArray values = getter.get(getProps().getQStr());

			// for each record do
			for (int i=0; i<values.length(); i++) {
				JSONObject obj = values.getJSONObject(i);
				String info = String.format("No.%s: %s", i+1, obj);
				System.out.println(info);
			}

			// store data to remote server
			try {
				StoreUtil.store(getProps(), values);
				System.out.println("store records successful!");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} finally {
			super.end();
		}
	}
}
